<?php
/**
 * Created by PhpStorm
 * User: 龍zero
 * Date: 2022-04-10
 * Time: 16:28
 */

declare(strict_types=1);

namespace App\Controller\Cms;

use App\Annotation\AdminRequired;
use App\Annotation\Logger;
use App\Annotation\LoginRequired;
use App\Annotation\RefreshRequired;
use App\Controller\AbstractController;
use App\Lib\Tools\LocalUser;
use App\Request\Users\ChangePasswordRequest;
use App\Request\Users\RegisterRequest;
use App\Request\Users\UpdateUserRequest;
use App\Service\Contract\ITokenProviderService;
use App\Request\Users\LoginRequest;
use App\Service\GroupService;
use App\Service\UserService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Annotation\PutMapping;
use Hyperf\RateLimit\Annotation\RateLimit;


/**
 * @Controller(prefix="cms/user")
 * Class AdminController
 * @package App\Controller\Cms
 */
class UserController extends AbstractController
{
    /**
     * @Inject
     * @var ITokenProviderService
     */
    private $token;

    /**
     * @Inject()
     * @var UserService
     */
    private $userService;

    /**
     * @Inject()
     * @var GroupService
     */
    private $groupService;

    /**
     * 注册用户
     * @AdminRequired
     * @PostMapping("register")
     * @param RegisterRequest $request
     */
    public function register(RegisterRequest $request)
    {
        $params = $request->post();
        $this->userService->createUser($params);
        return success(201, '用户注册成功', 11);
    }


    /**
     * @GetMapping("refresh")
     * @RefreshRequired
     */
    public function refresh()
    {
        return $this->token->refreshToken();
    }


    /**
     * 用户登录
     * @PostMapping("login")
     * @param LoginRequest $request
     * @return mixed
     * @Logger(template="{user.username}领取了登录令牌")
     */
    public function login(LoginRequest $request)
    {
        $params = $request->post();
        $user = $this->userService->getUserByUsername($params['username'], $params['password']);
        LocalUser::setLocalUser($user);
        return $this->token->getToken($user);
    }

    /**
     * 查询拥有权限
     * @GetMapping("permissions")
     * @LoginRequired
     */
    public function getPermissions(){
        $localUser = LocalUser::getLocalUser();
        $user = $localUser->toArray();
        $user['admin'] = false;
        $rootUserIds = $this->userService->getRootUserIds();
        if(in_array($user['id'],$rootUserIds)){
            $user['admin'] = true;
            $user['permissions'] = [];
        }else{
            $user['admin'] = false;
            $user['permissions'] = $this->userService->getStructuralUserPermissions($user['id']);
        }
        return $user;
    }

    /**
     * 查询自己信息
     * @LoginRequired
     * @GetMapping("information")
     */
    public function getInformation(){
        $localUser = LocalUser::getLocalUser();
        $user = $localUser->toArray();
        $groups = $this->groupService->getUserGroupsByUserID($user['id']);
        $user['groups'] = $groups;
        return $user;
    }

    /**
     * @PutMapping("")
     * @LoginRequired
     * @param UpdateUserRequest $request
     */
    public function update(UpdateUserRequest $request){
        $params = $request->post();
        $this->userService->updateUserInfo($params);
        return success(201,'更新用户成功',6);
    }

    /**
     * @PutMapping("change_password")
     * @LoginRequired()
     * @param ChangePasswordRequest $request
     */
    public function updatePassword(ChangePasswordRequest $request){
        $params = $request->post();
        $this->userService->changeUserPassword($params);
        return success(201,'密码修改成功',4);
    }

    /**
     * 获取登录验证码
     * @GetMapping("captcha")
     * @RateLimit(create=10, consume=1 ,capacity=30)
     */
    public function captcha(){
        return $this->userService->getCaptcha();
    }
}